package com.hxd.otc.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.utils.DataUtils;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/6/9 0009.
 * description ${保证金记录条目}
 */
public class CashDepositItemView extends ConstraintLayout {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.tv_money)
    TextView mTvMoney;

    private int mPlusColor;
    private int mMinusColor;
    private String mDateFormat;
    private String mMoneyFormat;


    public CashDepositItemView(Context context) {
        this(context, null);
    }

    public CashDepositItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CashDepositItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CashDepositItemView, defStyleAttr, 0);
        CharSequence title = a.getText(R.styleable.CashDepositItemView_title);
        String dateFormat = a.getString(R.styleable.CashDepositItemView_dateFormat);
        String moneyFormat = a.getString(R.styleable.CashDepositItemView_moneyFormat);
        int plusColor = a.getColor(R.styleable.CashDepositItemView_plusColor, getResources().getColor(R.color.color_FFD2A56B));
        int minusColor = a.getColor(R.styleable.CashDepositItemView_minusColor, getResources().getColor(R.color.color_FF464E5F));
        a.recycle();

        LayoutInflater.from(context).inflate(R.layout.view_cash_deposit_item, this);
        setBackgroundResource(R.drawable.selector_item_click);
        setPadding(dp2px(10), dp2px(13), dp2px(10), dp2px(14));
        ButterKnife.bind(this);

        setTitle(title);
        setDateFormat(dateFormat);
        setMoneyFormat(moneyFormat);
        setPlusColor(plusColor);
        setMinusColor(minusColor);
    }

    private void setMinusColor(int minusColor) {
        mMinusColor = minusColor;
    }

    public void setPlusColor(int plusColor) {
        mPlusColor = plusColor;
    }

    public void setMoney(double money, String format) {
        mMoneyFormat = format;
        setMoney(money);
    }

    public void setMoney(double money) {
        String format;
        int color;
        if (money < 0) {
            format = "%s";
            color = mMinusColor;
        } else {
            format = "+%s";
            color = mPlusColor;
        }
        mTvMoney.setTextColor(color);
        mTvMoney.setText(String.format(format, getDecimalFormat().format(money)));
    }

    private DecimalFormat getDecimalFormat() {
        return new DecimalFormat(mMoneyFormat);
    }

    public void setTime(String time) {
        mTvTime.setText(time);
    }


    public void setTitle(CharSequence title) {
        mTvTitle.setText(title);
    }

    private int dp2px(int dpValue) {
        return DataUtils.dp2px(getContext(), dpValue);
    }

    public void setDateFormat(String dateFormat) {
        this.mDateFormat = TextUtils.isEmpty(dateFormat) ? "yyyy-MM-dd HH:mm:ss" : dateFormat;
    }

    public void setMoneyFormat(String moneyFormat) {
        this.mMoneyFormat = TextUtils.isEmpty(moneyFormat) ? "#0.00" : moneyFormat;
    }

}
